﻿package org.nickro.project.utils{
	import com.adobe.utils.StringUtil;
	
    public class Strings {

        private static var sEmailExpression:RegExp;

        public function Strings(){
            throw (new Error("ERROR org.nickro.project.utils.Strings constructor: Class never intended to be instantiated"));
        }
		/**
		 * 返回一个字符串包含另一字符串的个数
		 * @param	big		对象字符串
		 * @param	small	包含字符串
		 * @param	offset	字符串开始位置
		 * @param	length	对象字符串长度
		 * @return	包含的个数
		 */
        public static function countOccurrences(big:String, small:String, offset:Number=0, length:Number=NaN):Number{
            if ((!(big is String)) || (!(small is String)) || (big == "") || (small == "")){
                return (0);
            };
            if (isNaN(length)){
                length = big.length;
            };
            big = big.slice(offset, length);
            return ((big.split(small).length - 1));
        }
		/**
		 * 返回一个字符串是否以另一个字符串开始
		 * @param	s
		 * @param	start
		 * @return	结果
		 */
		public static function startsWith(s:String, start:String):Boolean{
            if ((s == null) || (start == null) || (s.length == 0) || (start.length == 0)){
                return (false);
            };
            return ((s.indexOf(start) == 0));
        }
		/**
		 * 返回一个字符串是否以另一个字符串结束
		 * @param	s
		 * @param	end
		 * @return	结果
		 */
        public static function endsWith(s:String, end:String):Boolean{
            if ((s == null) || (end == null) || (s.length == 0) || (end.length == 0)){
                return (false);
            };
            var i:int = s.lastIndexOf(end);
            return ((i >= 0) && (i == (s.length - end.length)));
        }
		
		public static function padTrimStart(s:String, pad:String, len:Number, trim:Boolean=true):String{
            while (s.length < len) {
                s = (pad + s);
            };
            return ((trim) ? s.substr(-(len), len) : s);
        }
        public static function padTrimEnd(s:String, pad:String, len:Number, trim:Boolean=true):String{
            while (s.length < len) {
                s = (s + pad);
            };
            return ((trim) ? s.substring(0, len) : s);
        }
		
		/**
		 * 返回字符串是否包含空格
		 * @param	s	文本
		 * @return
		 */
        public static function isWhiteSpace(s:String):Boolean{
            return ((((((s == null)) || ((s == "")))) || ((StringUtil.trim(s) == ""))));
        }
		/**
		 * 返回字符串是否为合法邮件地址
		 * @param	email	email地址
		 * @return	验证是否成功
		 */
        public static function isValidEmail(email:String):Boolean{
            if (!(sEmailExpression is RegExp)){
                sEmailExpression = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
            };
            email = StringUtil.trim(email);
            return (sEmailExpression.test(email));
        }
    }
}
